/**
 * 
 */
package com.sip.comserver.communication.test;

import java.io.IOException;
import java.util.List;


import com.sip.comserver.communication.CommunicationMessage;
import com.sip.comserver.communication.ConnectionThread;
import com.sip.comserver.communication.JobListner;
import com.sip.comserver.communication.utils.CommonUtils;
import com.thoughtworks.xstream.XStream;

/**
 * @author Dhanushka Ariyasingha.
 */
public class SampleJobListner extends JobListner {

    public SampleJobListner() throws IOException {

        super();
        // TODO Auto-generated constructor stub
    }

    public void startActionListner() throws Exception {

        while (true) {

            if (hasJob()) {

                System.out.println("Found new job.");
                
                final List<ConnectionThread> connectionList = getConnectionList();

                for (final ConnectionThread connectionThread : connectionList) {

                    pick(connectionThread);

                    final CommunicationMessage message = recieveMessage(connectionThread);
                    
                    invoke(message);
                    
                    sendMessage(connectionThread, message);
                    
                }
                
                finalize();

            }
        }

    }




    private void invoke(final CommunicationMessage message) throws Exception {
        
/*        // JOBType:StoreFront|ID:dev44.aepona.com
        final String messageHeaderString = message.getMessageHeader();
        final String jobType = CommonUtils.find(messageHeaderString, "JOBType");
        
        if(jobType.contains("StoreFront")){
            
            final XStream xstream = new XStream();
            final String xml = xstream.toXML(message);
            
            System.out.println(xml);
            
            message.setObject(xml);
            
            
        }*/
        
        final String valueString = message.getMessageBody();
//        final StoreFrontTask storFronTask = new StoreFrontTask();
//        storFronTask.updateDataMap(valueString);
//
//        final String xml = storFronTask.getStoreFront();
        final String xml ="sample";
        message.setObject(xml);

        // TODO : Action to be perform should be written here.
    }

    public void finalize() throws IOException {

        clear();
    }

}
